/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package gameserver;
import gameserver.api.Output;
import gaps.utils.interaction.*;
import gameserver.User.*;
/**
*
* @author Jorge
*/
public class ConsistencyChecker implements Runnable {
private int period;
public ConsistencyChecker(int period){
this.period = period;
}
public ConsistencyChecker init(){
Thread t = new Thread(this);
t.setDaemon(true);
t.start();
return this;
}
public void run(){
long initTime;
long totalTime;
try{
while(true){
PokerServer server = PokerServer.getInstance();
synchronized(server){
Output.debug("Checking consistency...");
initTime = System.currentTimeMillis();
// user properties
for(User u: server.getUsers()){
User u2 = server.getUser(u.username);
if(u != u2)
throw new Exception(
"1 - Users should be the same");
if(u.level == UserLevel.NONE)
throw new Exception(
"2 - User in server.users with UserLevel.NONE");
if(!u.username.matches("[a-zA-Z0-9_]+"))
throw new Exception(
"3 - User in server.users username is not alphanumeric");
for(TablePlayer tp: u.getPlayers()){
if(tp.user != u)
throw new Exception(
"4 - User in player is not the same user that has the player");
if(tp.seat != tp.table.findSeat(u))
throw new Exception(
"5 - User-player seat is not the actual seat in the Table");
}
for(Table t: u.getPlayersTables()){
if(t.findSeat(u) < 0)
throw new Exception(
"6 - User has a player-table but table doesn't have player-user");
if(!t.getUsers().contains(u))
throw new Exception(
"7 - User has a player-table but table doesn't have user");
if(!t.getUsernames().contains(u.username))
throw new Exception(
"8 - User has a player-table but table doesn't have username");
}
}
for(Table t: server.getTables()){
Table t2 = server.getTable(t.tablename);
if(t != t2)
throw new Exception(
"9 - tables should be the same");
if(t.getUsers().isEmpty())
throw new Exception(
"10 - tables should never be empty");
if(t.getDealer() != null && t.getDealer() instanceof User){
if(!t.getUsers().contains((User)t.getDealer()))
throw new Exception(
"11 - Dealer in table is not in table users");
if(!((User)t.getDealer()).hasDealerLevel())
throw new Exception(
"12 - Dealer in table doesn't have permission to be a dealer");
}
// TODO: meter mais coisas, mas a mesa ainda nao esta acabada...
}
}
totalTime = System.currentTimeMillis() - initTime;
Output.debug(
"Consistency verified in " + totalTime / 1000 +
" seconds: No errors found. Consistency chek will start again in " +
period / 1000 + " seconds.");
// sleeps for some time...
ThreadUtils.sleep(period);
}
}catch(Exception e){
Output.error(e);
}
}
}